//
//  Extension+UIImage.swift
//  YMWeiBo
//
//  Created by mimi on 16/7/28.
//  Copyright © 2016年 mimi. All rights reserved.
//

import UIKit


extension UIImage{
    
    static func getScreenShot() ->UIImage{
        
        let windows = UIApplication.sharedApplication().keyWindow!
        //开启上下文
        
        UIGraphicsBeginImageContext(windows.bounds.size)
        
        //        // 01参数: size  02参数 是否透明 03参数 倍数【0,1】
        //        UIGraphicsBeginImageContextWithOptions(windows.bounds.size, false, 0)
        // 02当windows渲染到上下文中
        windows.drawViewHierarchyInRect(windows.bounds, afterScreenUpdates: false)
        //从上下文获取图片
        let image = UIGraphicsGetImageFromCurrentImageContext()
        
        //关闭上下文
        UIGraphicsEndImageContext()
        
        return image
    }
    
    func getImageScale(width: CGFloat)->UIImage{
        
        if self.size.width < width {
            
            return self
        }
        
        let height = (width*self.size.height)/self.size.width
        
        let rect = CGRect(x: 0, y: 0, width: width, height: height)
        
        UIGraphicsBeginImageContext(rect.size)
        // 02 将图片渲染到上下文中
        self.drawInRect(rect)
        // 03 从上下文获取image
        let result = UIGraphicsGetImageFromCurrentImageContext()
        // 04 关闭上下文
        UIGraphicsEndImageContext()
        return result
        
    }
    
    // 获得emotion 的image
    class func getEmoticonImage(path: String?) -> UIImage?{
        return UIImage(named: path ?? "", inBundle: YMEmoticonTools.sharedTools.enmoticonBundle, compatibleWithTraitCollection: nil)
    }
    
    
}
